package com.wee.fragment.find;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.bigtotoro.widgets.AppProgressBar;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wee.adapter.CoachAdapter1;
import com.wee.aircoach_user.CoachInfoActivity;
import com.wee.aircoach_user.Constant;
import com.wee.aircoach_user.MyApplication;
import com.wee.aircoach_user.R;
import com.wee.entity.Coach_list;
import com.wee.entity.Coach_listResponse;
import com.wee.entity.MD5Util;
import com.wee.entity.TrainDetails;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by STS on 2016/4/5.
 */
public class FindFragment extends Fragment{
    private View view;
    private ImageView iv_course;
    private ListView systemCoachList;
    private CoachAdapter1 coachAdapter1;
     private List<TrainDetails.DataBean> lists=new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

    {
        view = inflater.inflate(R.layout.fragment_find, container, false);
        initview();
        return view;
    }
    private void initview() {
        systemCoachList = (ListView)view.findViewById(R.id.list_0);
        /***
         * 发现头图的点击跳转
         */
        iv_course = (ImageView) view.findViewById(R.id.iv_course);
        iv_course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(getActivity(),CourseActivity.class);
                startActivity(intent);
            }
        });

        coachAdapter1 = new CoachAdapter1(getActivity(), lists);
        systemCoachList.setAdapter(coachAdapter1);
        systemCoachList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getActivity(), CoachInfoActivity.class);
                intent.putExtra("Coachid", lists.get(position).getId() + "");
                startActivity(intent);
            }
        });
        loadSystemCoac();
    }

    /***
     * 网络请求
     */
    private void loadSystemCoac(){
        int exact=  SharedPreferencesUtil.getInt(getActivity(), Constant.USERTIME);
        RequestParams params = new RequestParams();
        AppProgressBar.checkAndCreateProgressBar(getActivity());
        MyApplication.getInstance().getHttpClient().get(Constant.COACH_LIST+ MD5Util.md5(exact), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try{
                    Coach_listResponse response = new Gson().fromJson(new String(bytes), Coach_listResponse.class);
                    if (response != null && response.getData().size() != 0) {
                        lists.clear();
                        lists.addAll(response.getData());
                        coachAdapter1.notifyDataSetChanged();
                    }
                    AppProgressBar.closeProgressBar();

                }catch (Exception r){
                    Toast.makeText(getActivity(), "网络不稳定", Toast.LENGTH_SHORT).show();
                    AppProgressBar.closeProgressBar();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                AppProgressBar.closeProgressBar();
            }
        });
    }
}
